package pl.edu.mimuw.mas.editor;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pl.edu.mimuw.mas.chart.Chart;
import pl.edu.mimuw.mas.editor.listener.CreateChartButtonListener;

/**
 * Implementacja okna dialogowego do tworzenia nowej mapy.
 */
public class NewChartDialog extends Dialog {

	private static final int COLUMNS_MAX = 1500;
	private static final int COLUMNS_DEFAULT = 300;
	private static final int ROWS_MAX = 1500;
	private static final int ROWS_DEFAULT = 300;
	
	private static final int DIALOG_X_SIZE = 143;
	private static final int DIALOG_Y_SIZE = 162;
	
	private static final int DISTANCE = 10;

	private final Logger log = LoggerFactory.getLogger(getClass());
	
	private Button createChartButton;
	private Spinner distanceSpinner;
	private Spinner rowsSpinner;
	private Spinner columnsSpinner;
	
	protected Object result;
	protected Shell shell;

	/**
	 * Tworzy obiekt okna dialogowego.
	 * @param parent rodzic obiektu
	 * @param style styl
	 */
	public NewChartDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Tworzy obiekt okna dialogowego.
	 * @param parent rodzic obiektu
	 */
	public NewChartDialog(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Otwiera okno dialogowe.
	 * @return nowo stworzony obiekt mapy ({@link pl.edu.mimuw.mas.chart.Chart Chart})
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}
	
	/**
	 * Zamyka okno.
	 */
	public void closeShell() {
		shell.close();
	}
	
	/**
	 * Tworzy zawartość okna dialogowego (layouty, pola tekstowe, etc).
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new GridLayout());
		shell.setSize(DIALOG_X_SIZE, DIALOG_Y_SIZE);
		shell.setText("Nowa mapa");

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		final GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 2;
		composite.setLayout(gridLayout1);

		final Label columnsLabel = new Label(composite, SWT.NONE);
		columnsLabel.setLayoutData(new GridData());
		columnsLabel.setAlignment(SWT.RIGHT);
		columnsLabel.setText("Kolumny:");

		columnsSpinner = new Spinner(composite, SWT.BORDER);
		columnsSpinner.setMaximum(COLUMNS_MAX);
		columnsSpinner.setSelection(COLUMNS_DEFAULT);
		columnsSpinner.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));

		final Label rowsLabel = new Label(composite, SWT.NONE);
		rowsLabel.setAlignment(SWT.RIGHT);
		rowsLabel.setText("Wiersze:");

		rowsSpinner = new Spinner(composite, SWT.BORDER);
		rowsSpinner.setMaximum(ROWS_MAX);
		rowsSpinner.setSelection(ROWS_DEFAULT);
		rowsSpinner.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));

		final Label distanceLabel = new Label(composite, SWT.NONE);
		distanceLabel.setLayoutData(new GridData());
		distanceLabel.setAlignment(SWT.RIGHT);
		distanceLabel.setText("Dystans:");

		distanceSpinner = new Spinner(composite, SWT.BORDER);
		distanceSpinner.setSelection(DISTANCE);
		distanceSpinner.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		distanceSpinner.setPageIncrement(1);
		distanceSpinner.setMaximum(DISTANCE);
		distanceSpinner.setDigits(1);
		new Label(composite, SWT.NONE);

		createChartButton = new Button(composite, SWT.NONE);
		createChartButton.addSelectionListener(new CreateChartButtonListener(this));
		createChartButton.setText("Stwórz");
		composite.setTabList(new Control[] {columnsSpinner, rowsSpinner, distanceSpinner, createChartButton});
		
		createChartButton.setFocus();
	}
	
	/**
	 * Tworzy nowy obiekt klasy {@link pl.edu.mimuw.mas.chart.Chart Chart}
	 * na podstawie wartości wybranych w polach wyboru i ustawia lokalną
	 * zmienną result.
	 */
	public void createAndSetChart() {
		int rows = rowsSpinner.getSelection();
		int columns = columnsSpinner.getSelection();
		Double distanceDouble = (Double) (distanceSpinner.getSelection() / Math.pow(DISTANCE, distanceSpinner.getDigits()));
		float distance = distanceDouble.floatValue();
		log.info("Tworzę mapę z " + columns + " kolumnami " + rows
				+ " wierszami " + "i dystancem " + distance);
		Chart chart = new Chart(columns, rows, distance);
		result = chart;
	}

}
